package com.zbcz.erp.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.zbcz.erp.ErpApplication
import com.zbcz.erp.bean.DoingBusinessBean
import com.zbcz.erp.bean.MyToolsBean
import com.zbcz.erp.network.ErpApi
import com.zbcz.erp.network.RetrofitManager
import com.zbcz.erp.sp.SharedPreferencesManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MyToolsViewModel : ViewModel() {

    private val mApi = RetrofitManager.getRetrofit()?.create(ErpApi::class.java)

    var myToolsBeanLiveData: MutableLiveData<List<MyToolsBean?>?> = MutableLiveData()

    fun getMyToolsBeanList() {
        viewModelScope.launch {
            withContext(Dispatchers.IO) {
                val result = getMyToolsBeanListSuspend()
                myToolsBeanLiveData.postValue(result)
            }
        }
    }

    private suspend fun getMyToolsBeanListSuspend(): List<MyToolsBean>? {
        try {
            //token = SharedPreferencesManager.getToken(ErpApplication.mApplication)
            val result =
                mApi?.getToolsCollections()
            if (result?.code == 200) {
                return result.result
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }


    override fun onCleared() {
        super.onCleared()
    }
}